package com.nic.view.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;


import com.nic.view.R;
import com.nic.view.util.JToast;
import com.nic.view.util.glide.GlideUtil;
import com.nic.view.util.Utility;
import com.nic.view.widget.LookLargePhotoDialog;
import com.nic.view.widget.NiceImageView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/6.
 */

public class MultPhotoAdapter extends CommonAdapter<String> {
    private int tag;

    private OnClickItemListener listener;
    private Map<Integer, String> checkedMap = new HashMap<>();

    @Override
    public View getBindView(final int position, View convertView, final ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_mult_photo, null);
            holder = new ViewHolder();
            holder.imageView = (NiceImageView) convertView.findViewById(R.id.iv_photo);
            holder.checkBox = (CheckBox) convertView.findViewById(R.id.cb);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final String path = getList().get(position);
        holder.imageView.setTag(R.id.glide_image_id, path);
        String tag = (String) holder.imageView.getTag(R.id.glide_image_id);

        try {
            if (!TextUtils.isEmpty(tag) && tag == path) {
//                GlideUtil.getInstance().load(path, holder.imageView);
//                Bitmap bitmap = PhotoUtils.getImageThumbnail(path, 480, 480);
//                holder.imageView.setImageBitmap(bitmap);
                GlideUtil.getInstance().loadSmall(path, holder.imageView);
                //图片展示
                holder.imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        jumpPreview(parent, path);
                    }
                });
            } else {
                GlideUtil.getInstance().clear(holder.imageView);
                holder.imageView.setImageResource(R.drawable.ic_main_null);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (checkedMap.size() >= 9) {
                    if (isChecked) {
                        holder.checkBox.setChecked(false);
                    } else {
                        holder.checkBox.setChecked(true);
                    }
                    JToast.show(parent.getContext(), "选取不能超过九张图片", Toast.LENGTH_SHORT);
                    return;
                }
                if (isChecked) {
                    if (!checkedMap.containsKey(position)) {
                        checkedMap.put(position, path);
                    }
                } else {
                    if (checkedMap.containsKey(position)) {
                        checkedMap.remove(position);
                    }
                }
                try {
                    if (listener != null) {
                        listener.checkedItem(getCheckedPaths());
                    }
                    notifyDataSetChanged();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        if (checkedMap.containsKey(position)) {
            holder.checkBox.setChecked(true);
        } else {
            holder.checkBox.setChecked(false);
        }
        return convertView;
    }

    public void remove(int position) {
        getList().remove(position);
        notifyDataSetChanged();
    }

    private void jumpPreview(ViewGroup parent, String path) {
//        Intent intent = new Intent(parent.getContext(), PhotoPreviewActivity.class);
//        intent.putExtra(PhotoPreviewActivity.IMG, path);
//        parent.getContext().startActivity(intent);
        try {
            LookLargePhotoDialog dialog = new LookLargePhotoDialog(parent.getContext());
            GlideUtil.getInstance().load(path, dialog.getImageView());
            dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setTag(int tag) {
        this.tag = tag;
    }

    public int getTag() {
        return tag;
    }

    /*全选*/
    public void selectedAll() {
        for (int i = 0; i < getList().size(); i++) {
            String path = getList().get(i);
            checkedMap.put(i, path);
        }
        if (listener != null) {
            listener.checkedItem(getCheckedPaths());
        }
        notifyDataSetChanged();
    }


    /*反选*/
    public void unSelected() {
        checkedMap.clear();
        if (listener != null) {
            listener.checkedItem(getCheckedPaths());
        }
        notifyDataSetChanged();
    }

    public List<String> getCheckedPaths() {
        List<String> list = new ArrayList<>();
        if (checkedMap.isEmpty()) {
            return list;
        }
        checkedMap = Utility.sortMapByKey(checkedMap);//排序
        for (Map.Entry<Integer, String> entry : checkedMap.entrySet()) {
            list.add(entry.getValue());
        }
        return list;
    }

    public interface OnClickItemListener {
        void checkedItem(List<String> checkedPaths);
    }

    class ViewHolder {
        NiceImageView imageView;
        CheckBox checkBox;
    }

}
